var md5 = require('md5');
const express = require('express')
const adminModel = require('../db/modle/adminModel')
const isLoginMiddle = require('../middleware/isLoginMiddle')
const route = express.Router()
//引入正则校验中间件
const addAdminReg = require("../middleware/addAdminReg")
//添加管理员接口(添加正则校验中间件)
route.post('/addAdmin',isLoginMiddle,addAdminReg,async (req,res)=>{
    try {
        const {
            adminName,
            password,
            regTime
        } = req.body
        const findResult = await adminModel.findOne({
            adminName
        })
        if(findResult){
            return res.send({
                code: 10001,
                msg: '添加管理员-用户已经注册',
                data: null
            })
        }
        await adminModel.create({
            adminName,
            password: md5(password+66),
            regTime
        })
        res.send({
            code: 10000,
            msg: '添加管理员-添加成功',
            data: null
        })
    } catch (e) {
        console.log('添加管理员异常'+e.message);
        res.send({
            code: 10002,
            msg: '添加管理员-添加异常',
            data: null
        })
    }
})

module.exports = route;